﻿package com.bj58.dsap.hbasescf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bj58.dsap.api.scf.contract.Condition;
import com.bj58.dsap.api.scf.contract.IHbaseApi;
import com.bj58.dsap.api.scf.contract.Result;
import com.bj58.spat.scf.client.SCFInit;
import com.bj58.spat.scf.client.proxy.builder.ProxyFactory;

public class HBaseScfTest {

	static IHbaseApi service;
	static{
	     String configPath = System.getProperty("user.dir") + "/config/scf.config";
	     System.out.println(configPath);
	        SCFInit.init(configPath);
	        String url = "tcp://dsapapi/HbaseApi";
	        service = ProxyFactory.create(IHbaseApi.class, url);
	}
	
	public static void main(String[] args) {
		
		/**通过rowkey查询单条记录**/
		Result result = service.queryByRowKey("demo", "row-000");
		System.out.println("\n------------------通过rowkey查询单条记录---------------------");
		System.out.println(result.getScfMap());
		
		/**通过rowkey list查询多条记录**/
		List<String> rowkeyList = new ArrayList<String>();
		rowkeyList.add("row-001");
		rowkeyList.add("row-002");
		rowkeyList.add("row-003");
		rowkeyList.add("row-004");		
		List<Result> list = service.queryByRowKey("demo", rowkeyList);
		int count = 0;
		System.out.println("\n------------------通过rowkey list查询多条记录---------------------");
		for(Result rs : list) {
			System.out.println(++count + ":" + rs.getScfMap());
		}
		
		/**通过rowkey前缀查询 **/
		String prefix = "row-00";
		list = service.searchByPrefixFilter("demo", prefix);
		count = 0;
		System.out.println("\n------------------通过rowkey前缀查询 ---------------------");
		for(Result rs : list) {
			System.out.println(++count + ":" + rs.getScfMap());
		}
		
		/**通过rowkey前缀查询，只提供向下翻页**/
		prefix = "row-0";
		list = service.nextPagesByKeyPrefix("demo", prefix, "", 20);
		count = 0;
		String lastKey = "";
		System.out.println("\n------------------通过rowkey前缀查询，每页显示20条 ---------------------");
		for(Result rs : list) {
			System.out.println(++count + ":" + rs.getScfMap());
			lastKey = rs.getScfMap().get("rowKey").toString();
		}
		System.out.println("\n------------------通过rowkey前缀查询，每页显示20条， 从" + lastKey + "开始---------------------");
		list = service.nextPagesByKeyPrefix("demo", prefix, lastKey, 20);
		for(Result rs : list) {
			System.out.println(++count + ":" + rs.getScfMap());
			lastKey = rs.getScfMap().get("rowKey").toString();
		}

		
		/**通过列查询，需要组装列-值到Condition中**/		
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("cf:00", "00");
		//paramMap.put("cf:01", "01");
		Condition cond =  new Condition();
		cond.setScfMap(paramMap);
		list = service.nextPagesByColumnParams("demo", cond, "", 20);
		System.out.println("\n------------------通过列查询，每页显示20条 ---------------------");
		count = 0;
		for(Result rs : list) {
			System.out.println(++count + ":" + rs.getScfMap());
			lastKey = rs.getScfMap().get("rowKey").toString();
		}
		
		/**通过rowkey前缀分页查询，由于实现机制问题，数据量大的表，不建议使用**/
		prefix = "row-0";
		list = service.queryByPage("demo", prefix, 10, 2);
		System.out.println("\n------------------rowkey前缀分页查询，不建议使用 ---------------------");
		count = 0;
		for(Result rs : list) {
			System.out.println(++count + ":" + rs.getScfMap());
			lastKey = rs.getScfMap().get("rowKey").toString();
		}
				
		
		/** ----------------------------------------------------------- **/
		/** 通过rowkey和列数组组合查询，当一个列簇下有多列时，可以缩小查询范围，列使用 “列簇:列名”格式，存为数组**/
		System.out.println("------------------通过rowkey和列组合查询 ---------------------");
		String[] cfcqPairs = new String[] {"cf:16"};
		System.out.println(service.queryByRowKeyAndColumn("demo", "row-016", cfcqPairs).getScfMap());
		
		/** 通过rowkey范围翻页查询，如果了解自己的数据，知道范围内的记录条数，可以控制是否一次一页全部返回，如果数据量大，则需要翻页 **/
		/** 示例，查询row-000——row-020范围的数据，一次显示10条，**/
		/** 参数一次为：表名，起始rowkey，offset rowkey，终止rowkey，每页条数**/
		/** offset与起始rowkey相同时，结果集包含，起始rowkey，适用于第一页调用**/
		/** 翻页时，offset为上一页最后一条记录 **/		
		System.out.println("------------------通过rowkey范围查询---------------------");
		final String start = "row-000";
		String offset = start;
		final String end = "row-020";
		count = 0;
		while ((list = service.nextPagesByKeyInterval("demo", start, offset, end, 10)) != null) {
			for(Result rs : list) {
				System.out.println(++count + ":" + rs.getScfMap());
				offset = rs.getScfMap().get("rowKey").toString();
			}
		}
		
		System.out.println("demo OK!");

	}
}
